package org.example;

public class Test2 {
    //leetcode 669 修建二叉树 https://leetcode.cn/problems/trim-a-binary-search-tree/description/
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if (root == null) return null;
        if (root.val == low) {
            root.left = null;
            return root;
        }
        else if (root.val == high) {
            root.right = null;
            return root;
        }else if (root.val < low) return trimBST(root.right,low,high);
        else if (root.val > high) return trimBST(root.left,low,high);
        else {
            root.left = trimBST(root.left,low,high);
            root.right = trimBST(root.right,low,high);
            return root;
        }
    }
}
